package com.hzw.saas.service.schedule.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Sets;
import com.hzw.saas.api.schedule.IUserScheduleMeetingService;
import com.hzw.saas.api.schedule.bo.MeetingBo;
import com.hzw.saas.api.schedule.query.UserScheduleAddQuery;
import com.hzw.saas.api.schedule.query.UserScheduleMeetingAddQuery;
import com.hzw.saas.api.schedule.query.UserScheduleMeetingUpdateQuery;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.schedule.mapper.UserScheduleMeetingMapper;
import com.hzw.saas.service.schedule.model.UserSchedule;
import com.hzw.saas.service.schedule.model.UserScheduleMeeting;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class UserScheduleMeetingServiceImpl extends ServiceImpl<UserScheduleMeetingMapper, UserScheduleMeeting> implements IService<UserScheduleMeeting>, IUserScheduleMeetingService {

    @Resource
    private UserScheduleServiceImpl userScheduleService;

    @Override
    public Boolean addScheduleAndMeeting(UserScheduleMeetingAddQuery meetingAddQuery) {
        // 添加会议
        UserScheduleMeeting scheduleMeeting = MapperUtil.nf().map(meetingAddQuery, UserScheduleMeeting.class);
        scheduleMeeting.setCreatorId(meetingAddQuery.getUserId());
        scheduleMeeting.setPid(SnowFlakeUtil.nextIdStr());
        boolean meetingResult = super.save(scheduleMeeting);
        // 自动创建关联用户日程
        meetingAddQuery.getUserIds().add(meetingAddQuery.getUserId());
        UserScheduleAddQuery scheduleAdd = new UserScheduleAddQuery()
                .setUserId(meetingAddQuery.getUserId())
                .setTitle(meetingAddQuery.getMeetingTitle())
                .setContent(meetingAddQuery.getMeetingContent())
                .setMeetingId(scheduleMeeting.getPid())
                .setSkipWeekend(scheduleMeeting.getSkipWeekend())
                .setAheadTime(meetingAddQuery.getAheadTime())
                .setDueDate(meetingAddQuery.getDueDate())
                .setStartTime(meetingAddQuery.getStartTime())
                .setEndTime(meetingAddQuery.getEndTime())
                .setRepeatFlag(meetingAddQuery.getRepeatFlag());
        Boolean scheduleResult = userScheduleService.addBatchSchedule(scheduleAdd, Sets.newHashSet(meetingAddQuery.getUserIds()));
        return meetingResult && scheduleResult;
    }

    @Override
    public List<MeetingBo> getMeeting(String userId, String meetingId) {
        if (StrUtil.isBlank(meetingId)){
            return this.baseMapper.getMeetingByUser(userId);
        }else {
            UserScheduleMeeting scheduleMeeting = super.getById(meetingId);
            MeetingBo meetingBo = MapperUtil.nf().map(scheduleMeeting, MeetingBo.class);
            meetingBo.setIsCreator(userId.equals(scheduleMeeting.getCreatorId()));
            return Collections.singletonList(meetingBo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateMeeting(UserScheduleMeetingUpdateQuery updateQuery) {
        UserScheduleMeeting scheduleMeeting = super.getById(updateQuery.getMeetingId());
        if (!updateQuery.getUserId().equals(scheduleMeeting.getCreatorId())){
            //  非创建者 只能修改 本人任务提前量
            return userScheduleService.updateMeeting(updateQuery);
        }
        // 创建者 不改变其他人提前量
        UserScheduleMeeting meeting = MapperUtil.nf().map(updateQuery, UserScheduleMeeting.class);
        meeting.setPid(updateQuery.getMeetingId());
        return super.updateById(meeting) && userScheduleService.updateBatchMeetingSchedule(updateQuery);
    }

    @Override
    public Boolean deleteMeeting(String meetingId) {
        boolean result = super.removeById(meetingId);
        boolean update = userScheduleService.lambdaUpdate()
                .eq(UserSchedule::getMeetingId, meetingId)
                .remove();
        return result && update;
    }
}
